/******************************************
	commanderOrdered_Mech.sqf
	Commander Mechanized Infantry

	Quick converted from SQS.
*******************************************/

// -------------------------------------------------------------------------------------------------------------
// Verify target location
// -------------------------------------------------------------------------------------------------------------
_pos = [(_this select 0) select 0,(_this select 0) select 1,0];

if (surfaceIsWater [_pos select 0, _pos select 1])
	ExitWith{ player groupChat "Can't go there"; };

if ([(_pos select 0),(_pos select 1)] distance getmarkerpos _checker < 2400)
	ExitWith{ player groupChat "Not in range"; };


_cost = _this select 1;
_costic = _this select 2;
_costlc = _this select 3;
player sidechat "running xealot script";
// -------------------------------------------------------------------------------------------------------------
// Display spawn location dialog
// -------------------------------------------------------------------------------------------------------------
CloseScript = false;
PortSelected = false;


[] exec "player\OpenPortSelectionDialog.sqs";
sleep 0.3;


// Check harbor
while {!PortSelected} do {
	if (CloseScript)
		ExitWith{};
	sleep 0.2;
};

if (!PortSelected)
	ExitWith{};


onMapSingleClick "";


// -------------------------------------------------------------------------------------------------------------
// Create transport vehicle
// -------------------------------------------------------------------------------------------------------------
// Set side dependent variables
_checker = nil;
_MechVeh = nil;
_crewMan = nil;

if (side player == side0) then {
	_checker = format ["respawn_%1", side1];
	_MechVeh = side0_MechVeh;
	_crewMan = side0_crew;
} else {
	_checker = format ["respawn_%1", side0];
	_MechVeh = side1_MechVeh;
	_crewMan = side1_crew;
};


player groupChat "Mechanized moving out";

_basePos = [getmarkerPos PortSelectionPos select 0,getmarkerPos PortSelectionPos select 1,0];

// Create the transport/support vehicle
veh = createVehicle [_MechVeh, _basePos, [], 0, "NONE"];
sleep 0.2;
_veh = veh;

// -------------------------------------------------------------------------------------------------------------
// Create infantry squad and populate vehicle
// -------------------------------------------------------------------------------------------------------------

[_Veh,6] exec "player\spawnGroup.sqs";
sleep 3;

_crew = crew _Veh;
_grp = group (_crew select 0);
_groupSize = count _group;

// -------------------------------------------------------------------------------------------------------------
// Create crew and populate vehicle
// -------------------------------------------------------------------------------------------------------------
transportPilot = _grp createUnit [_crewMan, position player, [], 100, "FORM"];
transportGunner = _grp createUnit [_crewMan, position player, [], 100, "FORM"];

_unit = transportPilot;
_gunner = transportGunner;
[_unit,_gunner] join _grp;
_grp = group _unit;
_unit moveindriver _Veh;
_unit move _pos;
_gunner moveingunner _veh;
[_grp] exec "server\deleteGroup.sqs";


// -------------------------------------------------------------------------------------------------------------
// Create vehicle markers
// -------------------------------------------------------------------------------------------------------------

Add_MarkersGroup = _grp;
Add_MarkersType = "b_armor";
Add_Markers = true;

PublicVariable "Add_MarkersGroup";
PublicVariable "Add_MarkersType";
PublicVariable "Add_Markers";

markers = markers + [_grp];

// Deduct the cost from the side treasury
["payment",_cost,_costic,_costlc] exec "player\bought_commanderOrdered.sqs";
onMapSingleClick "";

// -------------------------------------------------------------------------------------------------------------
// Dismount infantry squad within 700 meters of target
// -------------------------------------------------------------------------------------------------------------

waitUntil {_veh distance _pos < 700};
_crew = crew _Veh;
_group = _crew - [driver _Veh, gunner _Veh, commander _unit];
_groupSize = count _group;

for [{_i=0}, {_i <= _groupSize}, {_i = _i+1}] do {
	_soldierMech = _group select _i;
	unassignVehicle _soldierMech;
	[_soldierMech] ordergetin false;
	sleep 0.5;
};